package com.loong.news.controller.forehead;

import com.loong.common.constant.NewsStatus;
import com.loong.common.mvc.BaseForeheadController;
import com.loong.common.param.BaseIdParam;
import com.loong.common.rest.Result;
import com.loong.common.validator.group.CommonValidate;
import com.loong.news.common.backend.param.NewsListQueryParam;
import com.loong.news.handler.NewsHandler;
import com.loong.news.param.NewsListParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Description: 处理前台发送新闻请求的Controller
 * @Author: 刘群
 * @Date: 2020/10/18 20:58
 */
@RestController
@RequestMapping("forehead/news/news")
public class NewsForeheadController extends BaseForeheadController {

    @Resource
    private NewsHandler newsHandler;

    /**
     * 阅读新闻
     * @param param 新闻id封装的参数
     * @return 响应的结果
     */
    @PostMapping("read")
    public Result readNews(@RequestBody @Validated({CommonValidate.class}) BaseIdParam param){
        return handlerExecutor.execute(() -> newsHandler.readNews(param.getId()));
    }

    /**
     * 分享新闻
     * @param param 新闻id封装的参数
     * @return 响应的结果
     */
    @PostMapping("share")
    public Result shareNews(@RequestBody @Validated({CommonValidate.class}) BaseIdParam param){
        return handlerExecutor.execute(() -> newsHandler.shareNews(param.getId()));
    }

    /**
     * 点赞新闻
     * @param param 新闻id封装的参数
     * @return 响应的结果
     */
    @PostMapping("like")
    public Result likeNews(@RequestBody @Validated({CommonValidate.class}) BaseIdParam param){
        return handlerExecutor.execute(() -> newsHandler.likeNews(param.getId()));
    }

    /**
     * 举报新闻
     * @param param 新闻id封装的参数
     * @return 响应的结果
     */
    @PostMapping("unlike")
    public Result unLikeNews(@RequestBody @Validated({CommonValidate.class}) BaseIdParam param){
        return handlerExecutor.execute(() -> newsHandler.unLikeNews(param.getId()));
    }

    /**
     * 更新新闻的阅读分享等数量
     * @param param 新闻id封装的参数
     * @return 响应的结果
     */
    @PostMapping("updateNewsNumFields")
    public Result updateNewsNumFields(@RequestBody @Validated({CommonValidate.class}) BaseIdParam param){
        return handlerExecutor.execute(() -> newsHandler.updateNewsNumFields(param.getId()));
    }
    /**
     * 分页查询已发布的新闻
     * @param param 新闻列表查询参数
     * @return 响应的结果
     */
    @PostMapping("newsList/pageQuery")
    public Result newsListPageQuery(@RequestBody  NewsListQueryParam param){
        param.setStatus(NewsStatus.RELEASED);//设置新闻为已发布
        return handlerExecutor.execute(() -> newsHandler.newsListToForeheadPageQuery(param));
    }

    /**
     * 根据新闻id查询新闻
     * @param param 新闻id封装的参数
     * @return 响应的结果
     */
    @PostMapping("preview")
    public Result previewNews(@RequestBody @Validated({CommonValidate.class}) BaseIdParam param){
        return handlerExecutor.execute(() -> newsHandler.findNewsById(param.getId()));
    }

}
